<script>
$(document).ready(function(){
  $("#selectGrade").change(function(){
      if ($(this).val() =='') {
       alert('先選擇年級');
       return;
       }
      $("#setForm").submit();
  })
   $("#selectYearSemester").change(function(){
	    $("#selectGrade").trigger('change');
	})

	$(".delete-semester").click(function(){
		var current = $(this);
	    var yearSemester = $(this).attr('id').substr(2);
	    // 查詢是否有學生設定
	    $.getJSON('<?php echo $this->route("check-students")?>',{
	        yearSemester : yearSemester
		    },function(result){
		        if (result)
			        alert('已有學生設定,不能刪除');
		        else {
			        if (confirm('確定刪除學期設定')) {
				        $.getJSON('<?php echo $this->route("delete-semester")?>',{
				            yearSemester : yearSemester
					       },function(result){
					    	    if (result)
						    	    current.parent().remove();
						   });
			        }
		        }
			});
	});

    $("#stepBtn").click(function(){
        var cc = $("INPUT[@name='selectClassSn[]'][type='checkbox']:checked");
        if (cc.length == 0) {
            alert('未選擇班級');
            return;
        }
        $.getJSON('<?php echo $this->route("check-file")?>',
            {
        	selectYearSemester : $("#selectYearSemester").val(),
        	selectGrade : $("#selectGrade").val()
            },
            function(result){
                if (result) {
                	alert('已有編班設定,請重新選擇班級');
                }
                else {
                    $("#hidden_save").attr('value',1);
                    $("#setForm").submit();
                }
            });

     });

  $("#checkAll").click(function(){
      $.each($("INPUT[@name='selectClassSn[]'][type='checkbox']"),function(){
          $(this).attr('checked', !$(this).is(':checked'));
      });
 });

});
</script>
<style>
.listTable {background:#9ebcdd; width:75%; margin: 5px auto;}
.listTable td {padding:3px;background:#fff; }
.listTable th {padding:3px;font-weight: bold; text-align:center;}
</style>
<form method="post" action="" name="setForm"  id="setForm">
<table class="listTable" >
<tr><th colspan=2 style="background: #fff"><?php echo  $this->translate('rand title')?>--1.編班設定
<input type="button"  value='返回主頁'  onclick="location.href='<?php echo $this->route('index')?>'" />
</th></tr>
<?php if ($this->semesterList):?>
<tr><th colspan=2>已存在之設定項目</th></tr>
<tr><td colspan=2>
<div style="height: 120px; overflow: auto; padding:5px">
<ul>
<?php $gradeName = unserialize($this->School['length_name']);?>
<?php foreach($this->semesterList as $val):?>
<li><?php $arr = explode('-',$val['year_semester'])?>
<?php echo sprintf($this->translate('%d year %d semester %s'),$arr[0], $arr[1], $gradeName[$arr[2]])?>
<input type="button"  id="s-<?php echo $val['year_semester']?>" value="刪除"  class="delete-semester" /></li>
<?php endforeach;?>
</ul>
</div>
</td></tr>
<?php endif?>
<tr>
<th>設定項目</th>
<th>項目內容</th>
</tr>
<tr style="background:#FFFFFF" >
<th>A.舊生來源</th>
<td>
<select id="selectYearSemester" name="selectYearSemester" >
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select name="selectGrade"  id="selectGrade">
<option value="">選擇編班年級</option>
<?php echo $this->ClassGradeOptions($this->grade)?>
</select>

</td>
</tr>
<?php if ($this->className):?>
<tr style="background:#FFFFFF" >
<th>B.在 <span id="selected-grade"></span> 舊生中，選擇參加班級
<input id="checkAll" type="button" value="全選/取消/反向"  />
</th>
<td>
<?php foreach($this->className as $val):?>
<?php $totalClass = count($val)?>
<?php foreach ($val as $id=>$name):?>
<input type="checkbox" name="selectClassSn[]" value="<?php echo $id?>"  id="c-<?php echo $id?>"/>
<label for="c-<?php echo $id?>"><?php echo $name?></label>
<?php endforeach;?>
<?php endforeach;?>
</td>
</tr>
<tr style="background:#FFFFFF" >
<th>C.編為幾班(新班級數)</th>
<td><input type="text" name="totalClass" value="<?php echo $totalClass?>" size="5" maxlength="5">
</td></tr>

<tr style="background:#FFFFFF; text-align:center" >
<td colspan="2">
<input type="hidden"  id="hidden_save" name="save"  />
<input type="button"  id="stepBtn" value='記錄目前設定，進行下一步驟' />
</td></tr>
<?php endif;?>
</table>
</form>
